package com.ichoice.rabbitmq.rpc.controller;

import cn.hutool.core.lang.Dict;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.ichoice.rabbitmq.rpc.User;
import com.ichoice.rabbitmq.rpc.helper.RpcRabbit;
import com.ichoice.rabbitmq.rpc.helper.model.CallBackModel;
import com.ichoice.rabbitmq.rpc.helper.model.ProducerModel;
import com.ichoice.rabbitmq.rpc.slove.Slove1;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
public class ClientTest {

    @Resource
    private RpcRabbit rpcRabbit;

    @Resource
    private Slove1 slove1;

    @PostMapping("test0")
    public void test0() {
        ProducerModel producerModel = ProducerModel.builder()
                .targetApplication("hsp-dict")
                .bean("loginImpl")
                .method("login")
                .args("xianrensawang", "greet")
                .build();
        rpcRabbit.sentAndBack(producerModel, slove1);
    }

    @PostMapping("test1")
    public void test1() {
        ProducerModel producerModel = ProducerModel.builder()
                .targetApplication("hsp-dict")
                .bean("loginImpl")
                .method("getName")
                .args(100)
                .build();
        rpcRabbit.sentBackByTargetMethod(producerModel, slove1, "test2");
    }


    @PostMapping("test2")
    public void test2() {
        ProducerModel producerModel = ProducerModel.builder()
                .targetApplication("hsp-dict")
                .bean("loginImpl")
                .method("getName")
                .args("100")
                .build();
        rpcRabbit.sentBackByTargetMethod(producerModel, slove1, "test2");
    }

    @PostMapping("test3")
    public void test3() {
        Dict dict = Dict.create().
                set("name", "pdd1").
                set("sex", "女");
        ProducerModel producerModel = ProducerModel.builder()
                .targetApplication("hsp-dict")
                .bean("loginImpl")
                .method("user")
                .args(dict)
                .build();
        rpcRabbit.sentBackByTargetMethod(producerModel, slove1, "test3");
    }

    @PostMapping("test4")
    public void test4() {
        ProducerModel producerModel = ProducerModel.builder()
                .targetApplication("hsp-dict")
                .bean("loginImpl")
                .method("getAge")
                .args(150)
                .build();
        rpcRabbit.sentBackByTargetMethod(producerModel, slove1, "test4");
    }


    @PostMapping("test5")
    public void test5() {
        User user = new User();
        user.setName("pdd001");
        user.setSex("男");
        ProducerModel producerModel = ProducerModel.builder()
                .targetApplication("hsp-dict")
                .bean("loginImpl")
                .method("user")
                .args(user)
                .build();
        rpcRabbit.sentBackByTargetMethod(producerModel, slove1, "test5");
    }

    @PostMapping("test6")
    public void test6() {
        List<User> userList = new ArrayList<>();
        User user = new User();
        user.setName("pdd001");
        user.setSex("男");
        userList.add(user);
        ProducerModel producerModel = ProducerModel.builder()
                .targetApplication("hsp-dict")
                .bean("loginImpl")
                .method("userList1")
                .args(userList)
                .build();
        CallBackModel callBackModel = rpcRabbit.sentAndBack(producerModel);
        List<User> userList1 = JSONUtil.parseArray(callBackModel.getRes()).toList(User.class);
        System.out.println(userList1.toString());
    }


    @PostMapping("test7")
    public void test7() {
        for (int i = 0; i < 10; i++) {
            ProducerModel producerModel = ProducerModel.builder()
                    .targetApplication("hsp-dict")
                    .bean("loginImpl")
                    .method("getAge")
                    .args(150L)
                    .build();
            CallBackModel callBackModel = rpcRabbit.sentAndBack(producerModel);
            System.out.println(callBackModel.getRes());
        }
    }


    @PostMapping("test8")
    public void test8() {
        ProducerModel producerModel = ProducerModel.builder()
                .targetApplication("hsp-dict")
                .bean("loginImpl")
                .method("getName")
                .args("150")
                .build();
        rpcRabbit.sent(producerModel);
    }

    @PostMapping("test9")
    public void test9() {
        ProducerModel producerModel = ProducerModel.builder()
                .targetApplication("hsp-dict")
                .bean("loginImpl")
                .method("login")
                .args("xianrensawang", "greet")
                .ttl(10000)
                .build();
        System.out.println("默认方法延迟回调测试");
        rpcRabbit.sentAndBack(producerModel, slove1);
    }


    @PostMapping("test10")
    public void test10(Integer msg) {
        ProducerModel producerModel = ProducerModel.builder()
                .targetApplication("hsp-dict")
                .bean("loginImpl")
                .method("getName")
                .args(100)
                .build();
        System.out.println("指定方法延迟回调测试");
        rpcRabbit.sentBackByTargetMethod(producerModel, slove1, "test2");
    }


    @PostMapping("test11")
    public void test11() {
        ProducerModel producerModel = ProducerModel.builder()
                .targetApplication("hsp-dict")
                .bean("loginImpl")
                .method("getAge")
                .args(100L)
                .retry(3)
                .retryDelay("100")
                .build();
        System.out.println("重试机制尝试");
        CallBackModel callBackModel = rpcRabbit.sentAndBack(producerModel);
        System.out.println(callBackModel.getRes());
    }


    @PostMapping("test12")
    public void test12(Integer msg) {
        ProducerModel producerModel = ProducerModel.builder()
                .targetApplication("hsp-dict")
                .bean("loginImpl")
                .method("getName")
                .args(msg)
                .ttl(msg)
                .build();
        System.out.println("方法阻塞测试");
        rpcRabbit.sentBackByTargetMethod(producerModel, slove1, "test2");
    }

    @PostMapping("test13")
    public void test13() {
        ProducerModel producerModel = ProducerModel.builder()
                .targetApplication("hsp-dict")
                .bean("loginImpl")
                .method("getAge")
                .args(1000)
                .retry(3)
                .retryDelay("100")
                .build();
        System.out.println("重试机制尝试");
        CallBackModel callBackModel = rpcRabbit.sentAndBack(producerModel);
        System.out.println(callBackModel.getRes());
    }
}
